package com.ruoyi.form.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.form.base.StringUtils;
import com.ruoyi.form.domain.AxModelField;
import com.ruoyi.form.service.IAxModelFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 【请填写功能名称】Controller
 *
 * @author LZF
 * @date 2025-07-18
 */
@RestController
@RequestMapping("/form/config/modelfield")
public class AxModelFieldController extends BaseController
{
    @Autowired
    private IAxModelFieldService axModelFieldService;

/**
 * 查询【请填写功能名称】列表
 */
@PreAuthorize("@ss.hasPermi('system:grid:list')")
@GetMapping("/list")
    public TableDataInfo list(AxModelField axModelField)
    {
        startPage();
        List<AxModelField> list = axModelFieldService.selectAxModelFieldList(axModelField);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:grid:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AxModelField axModelField)
    {
        List<AxModelField> list = axModelFieldService.selectAxModelFieldList(axModelField);
        ExcelUtil<AxModelField> util = new ExcelUtil<AxModelField>(AxModelField.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:grid:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(axModelFieldService.selectAxModelFieldById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:grid:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AxModelField axModelField)
    {
        return toAjax(axModelFieldService.insertAxModelField(axModelField));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:grid:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AxModelField axModelField)
    {
        return toAjax(axModelFieldService.updateAxModelField(axModelField));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:grid:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(axModelFieldService.deleteAxModelFieldByIds(ids));
    }
    /********************************************************************************************/
    /**
     * 查询模型对应筛选列表 + 表格配置 生成页面
     * @author LZF
     * @date 2025-06-05
     */
    @GetMapping(value = "/getConfigView")
    public AjaxResult getConfigView(String modelId, String type)
    {
        if (StringUtils.isEmpty(modelId)){
            return success();
        }
        if ("filter".equals(type)){
            return success(axModelFieldService.createFilterForm(modelId));
        } else if ("table".equals(type)) {
            QueryWrapper<AxModelField> wrapper = new QueryWrapper<>();
            wrapper.eq("model_id",modelId);
            wrapper.eq("show_flag","0");
            wrapper.orderByAsc("sort_num");
            List<AxModelField> list = axModelFieldService.list(wrapper);
            return success(list);
        }
        return AjaxResult.error("configFieldType error");
    }
    /**
     * 查询表格显示列表
     */
    //@PreAuthorize("@ss.hasPermi('config:gridConfig:list')")
    @GetMapping("/listContainMCode")
    public AjaxResult listContainMCode(AxModelField axModelField)
    {
        axModelField.setShowFlag("1");
        return AjaxResult.success(axModelFieldService.queryListAndPropertyName(axModelField));
    }
}
